<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/016
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class PartnerController extends Controller{
	/*
     * 列表
     */
    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');
        $page_size = I('post.page_size/d');

       

        $model = M('partner');

        $count = $model->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $result = $model->order('rank desc')->limit($limitStart,$page_size)->select();

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

   

     /*修改*/

     /*添加*/
    public function add(){

        $this->isAjaxLogin();//检查是否登录

        $id = I('post.id');

        if($id==null){

            $Model = M('partner');

            $picture   = I('post.litpic');

            $link    = I('post.name');
            $name    = I('post.key');
           
            $arr = $Model->select();
            foreach($arr as $v){
                $rank = $v['id']+1;
            }
            
            $data['picture']   = $picture;
            $data['link']   = $link;
            $data['name']   = $name;
           



             $data['rank']  = $rank;
           

            $result = $Model->add($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
            }

            $this->ajaxReturn($ajaxReturn);

        }else{

             $Model = M('partner');

            $id     = I('post.id/d');
            $picture   = I('post.picture');

            $link    = I('post.key');
            $name    = I('post.name');
           

            $data['picture']   = $picture;
            $data['link']   = $link;
            $data['name']   = $name;
           

            $result = $Model->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

           
          

            $this->ajaxReturn($ajaxReturn);


        }

        
    }

     /*
     * 删除
     */
    public function delete (){

        $ids = I('get.id');

        $model = M('partner');


        $ids = explode(',',$ids);

        foreach($ids as $id){
            $model-> where('id='.$id)->delete();
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }



    /*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('partner');

        $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('rank,name');
        $thisrank   = array_keys($thisinfo)[0];
        $thiscid    = array_values($thisinfo)[0];


        $nextinfo   = $ArticleModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('id,rank');
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $ArticleModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $ArticleModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

      /*
    详情
    */
    public  function infos(){
        $this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('partner');

        $result = $Model->where([
                'id'=>$id,
            ])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }
}